/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RegisterForm.java
 *
 * Created on 02.03.2010, 22:30:28
 */
package Client;

import java.awt.Component;
import java.util.Vector;

/**
 *
 * @author Mario
 */
public class RegisterForm extends javax.swing.JFrame
{

    private ClientImpl _client;

    /** Creates new form RegisterForm */
    public RegisterForm()
    {
        initComponents();
    }

    /** Creates new form RegisterForm */
    public RegisterForm(ClientImpl client)
    {
        this();

        _client = client;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        progBar = new javax.swing.JProgressBar();
        cbServer = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lblState = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtPlayerName = new javax.swing.JTextField();
        btnRegister = new javax.swing.JButton();
        btnForceGame = new javax.swing.JButton();
        btnUnregister = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 36));
        jLabel1.setText("Welcome To Alcatraz!");

        jLabel2.setText("Server");

        jLabel3.setText("State ");

        lblState.setText("waiting");

        jLabel5.setText("Player Name");

        txtPlayerName.setText("PlayerX");

        btnRegister.setText("Register");
        btnRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegisterActionPerformed(evt);
            }
        });

        btnForceGame.setText("Start Now!");
        btnForceGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnForceGameActionPerformed(evt);
            }
        });

        btnUnregister.setText("Unregister");
        btnUnregister.setEnabled(false);
        btnUnregister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUnregisterActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(progBar, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblState)))
                        .addGap(18, 18, 18)
                        .addComponent(btnRegister)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnForceGame)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnUnregister))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel5)
                            .addGap(18, 18, 18)
                            .addComponent(txtPlayerName))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addGap(46, 46, 46)
                            .addComponent(cbServer, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbServer, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtPlayerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(lblState))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(progBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnRegister, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnForceGame, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnUnregister, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(20, 20, 20))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegisterActionPerformed
    if (_client != null)
    {
        String serverName = String.valueOf(cbServer.getSelectedItem());
        if (_client.Register(serverName, txtPlayerName.getText()))
        {
            for (Component component : txtPlayerName.getParent().getComponents())   //Weiß nicht wie man sonst zu alle Controls kommt?!?!
            {
                if (!component.equals(btnUnregister))
                {
                    component.setEnabled(false);
                }
            }
            lblState.setText("Warte...");
            progBar.setIndeterminate(true);
            btnUnregister.setEnabled(true);
        }
    }
}//GEN-LAST:event_btnRegisterActionPerformed

private void btnUnregisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUnregisterActionPerformed
    if (_client != null)
    {
        String serverName = String.valueOf(cbServer.getSelectedItem());
        _client.Unregister(serverName);

        for (Component component : txtPlayerName.getParent().getComponents())   //Weiß nicht wie man sonst zu alle Controls kommt?!?!
        {
            component.setEnabled(true);
        }
        txtPlayerName.setText("PlayerX");
        lblState.setText("Warte...");
        progBar.setIndeterminate(true);
        btnUnregister.setEnabled(false);
    }
}//GEN-LAST:event_btnUnregisterActionPerformed

private void btnForceGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnForceGameActionPerformed
    if (_client != null)
    {
        String serverName = String.valueOf(cbServer.getSelectedItem());
        if (_client.ForceStartGame(serverName, txtPlayerName.getText()))
        {
            for (Component component : txtPlayerName.getParent().getComponents())   //Weiß nicht wie man sonst zu alle Controls kommt?!?!
            {
                if (!component.equals(btnUnregister))
                {
                    component.setEnabled(false);
                }
            }
            lblState.setText("Warte...");
            progBar.setIndeterminate(true);
            btnUnregister.setEnabled(true);
        }
    }
}//GEN-LAST:event_btnForceGameActionPerformed

    public void FillServerComboBox(Vector<String> list)
    {
        for (String server : list)
        {
            cbServer.addItem(server);
        }
    }
//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[])
//    {
//        java.awt.EventQueue.invokeLater(new Runnable()
//        {
//
//            public void run()
//            {
//                new RegisterForm().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnForceGame;
    private javax.swing.JButton btnRegister;
    private javax.swing.JButton btnUnregister;
    private javax.swing.JComboBox cbServer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel lblState;
    private javax.swing.JProgressBar progBar;
    private javax.swing.JTextField txtPlayerName;
    // End of variables declaration//GEN-END:variables
}
